package com.hhp.hhptt.controller.controllerjinpeng;

import com.hhp.hhptt.common.Result;
import com.hhp.hhptt.entity.ExamineeEntiry;
import com.hhp.hhptt.entity.ImagesEntity;
import com.hhp.hhptt.entity.TeacherXingongyuan;
import com.hhp.hhptt.service.IImagesService;
import com.hhp.hhptt.vo.ImagesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/image/images")
public class ImagesController {
    @Autowired
    IImagesService imagesService;

    @GetMapping("/list")
    //public Result ImagesInformation(ExamineeEntiry employee) throws ParseException {
    public Result ImagesInformation() throws ParseException {
        List<ImagesVo> images = new ArrayList<>();
        List<ImagesEntity> list = imagesService.list();
        // 定义输入日期格式
//        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 定义输出日期格式
        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy年MM月dd日");
        for (ImagesEntity imagesEntity : list) {
            ImagesVo imagesVo = new ImagesVo();

            imagesVo.setId(imagesEntity.getId());
            imagesVo.setTitle(imagesEntity.getTitle());
            //图片url

            String image = imagesEntity.getImage();
            String directory = "images/";
            String iconUrl = ServletUriComponentsBuilder.fromCurrentContextPath()
                    .path("/images/")
                    .path(directory)
                    .path(image)
                    .toUriString();

//            imagesEntity.setImage(iconUrl);
            imagesVo.setImage(iconUrl);

            imagesVo.setAuthor(imagesEntity.getAuthor());
            imagesVo.setDescribe(imagesEntity.getDescribe());
            imagesVo.setRichText(imagesEntity.getRichText());

            //设置时间
            // 解析输入日期字符串为Date对象
            Date date = imagesEntity.getTime();
            // 格式化Date对象为输出日期字符串
            String formattedDateStr = outputFormat.format(date);
            imagesVo.setTime(formattedDateStr);

            imagesVo.setBrowse(imagesEntity.getBrowse());
            imagesVo.setLike(imagesEntity.getLike());
            imagesVo.setIsLike(imagesEntity.getIsLike());
            images.add(imagesVo);
        }
        System.out.println("images:"+images.size());
        return Result.success("请求成功",images);
    }


    //@GetMapping("/details/{id}")
    @GetMapping("/details")
    //public Result ImagesDetails(@PathVariable Long id) throws ParseException {
    public Result ImagesDetails(@RequestParam Long id,@RequestParam Long uid) throws ParseException {
//        System.out.println("ssss");
        ImagesEntity imagesEntity = imagesService.getById(id);
        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy年MM月dd日");
        ImagesVo imagesVo = new ImagesVo();

        imagesVo.setId(imagesEntity.getId());
        imagesVo.setTitle(imagesEntity.getTitle());
        //图片url
        String image = imagesEntity.getImage();
        String directory = "images/";
        String iconUrl = ServletUriComponentsBuilder.fromCurrentContextPath()
                .path("/images/")
                .path(directory)
                .path(image)
                .toUriString();
        imagesVo.setImage(iconUrl);

        imagesVo.setAuthor(imagesEntity.getAuthor());
        imagesVo.setDescribe(imagesEntity.getDescribe());
        imagesVo.setRichText(imagesEntity.getRichText());

        //设置时间
        // 解析输入日期字符串为Date对象
        Date date = imagesEntity.getTime();
        // 格式化Date对象为输出日期字符串
        String formattedDateStr = outputFormat.format(date);
        imagesVo.setTime(formattedDateStr);

        imagesVo.setBrowse(imagesEntity.getBrowse());
        imagesVo.setLike(imagesEntity.getLike());
        imagesVo.setIsLike(imagesEntity.getIsLike());
//        System.out.println(imagesVo);
        return Result.success("请求成功",imagesVo);
    }


    @GetMapping("/look")
    //public Result ImagesDetails(@PathVariable Long id) throws ParseException {
    public Result ImagesLook(@RequestParam int id) throws ParseException {
        boolean result = imagesService.updateImagesBrowers(id);
        String result1="更新失败！";
        if (result){
            result1="更新成功！";
        }
        return Result.success(result1);
    }


    @GetMapping("/like")
    //public Result ImagesDetails(@PathVariable Long id) throws ParseException {
    public Result ImagesLike(@RequestParam int id,@RequestParam Long uid,@RequestParam int status) throws ParseException {
        boolean result = imagesService.updateImagesLike(id,uid,status);
        String result1="更新失败！";
        if (result){
            result1="更新成功！";
        }
        return Result.success(result1);
    }
}
